'use strict';
/**
 * split the database data out for easy upgrade later
 * scenario if the user chooce vagrant or docker for their development
 * then they will have a fix ip and credential to use
 */
var _ = require('lodash');

// var names = ['Name' , 'User' , 'Path' , 'Pass' , 'Port'];
var defaultPath = '192.168.33.15';
var localhost = '127.0.0.1';
var user = 'demo';
var pass = 'demo';
var dbname = 'panes';
var ports = {
		'mysql': 3306,
		'mariadb': 3306,
		'postgresql': 5432,
		'mssql': 1443,
		'mongodb': 27017,
		'couchdb': 5984,
		'redis': 6379
	};
var defaultSettings = {
		name: 'panes',
		user: null,
		pass: null
	};

/**
 * @param (boolean) virtual or not
 * @param (string) database type
 * @param (string) database driver
 */
var get = function(virtual , type , driver)
{
	var path = (virtual==='n') ? localhost : defaultPath;
	if (type==='NOSQL') {
		return _.extend( defaultSettings , {path: path , port: ports[driver] , type: type , driver: driver });
	}
	if (driver==='sqlite') {
		return _.extend( defaultSettings , {path: 'data/' + dbname + '.db' , type: type , driver: driver});
	}
	// finally
	return {
		type: type,
		driver: driver,
		name: dbname,
		user: user,
		pass: pass,
		port: ports[driver],
		path: path
	};
};

/**
 * construct data structure
 */
var construct = function(conf)
{
	var opt = {
		type: conf.type,
		driver: conf.driver
	};
	switch (conf.type) {
		case 'NOSQL':
			switch (conf.driver) {
				case 'mongodb':
					opt.uri = 'mongodb://' + conf.path + '/' + conf.name;
					opt.options = {
						user: conf.user,
						pass: conf.pass
					};
				break;
				case 'couchdb':
					opt.uri = conf.path;
					opt.options = {
						port: conf.port,
						// @TODO add extra question for this options when they are using couchdb
						cache: true,
                        raw: false,
                        forceSave: true
					};
				break;
				case 'redis':
					//	@TODO  this only apply to production, so we need to have a development split
					if (conf.user!==null && conf.pass!==null) {
						opt.uri = 'redis://' + conf.user +':' + conf.pass + '@' + conf.path + ':' + conf.port;
					}
					else {
						opt.uri = 'redis://' + conf.path + ':' + conf.port;
					}
					opt.options = {};
				break;
				default:
					// nothing here
			}
		break;
		case 'RDBMS':
			if (conf.driver === 'sqlite') {
            	opt.options = {};
            	opt.options.name = conf.name;
            	opt.options.user = conf.user;
            	opt.options.pass = conf.pass;
            	opt.options.path = conf.path;
            	opt.uri = '';
			}
			else {
				opt.uri = conf.driver + '://' + conf.user + ':' + conf.pass + '@' + conf.path + ':' + conf.port + '/' + conf.name;
				opt.options = {};
			}
		break;
		default:
			// nothing
			throw 'no type of driver!';
	}
	return opt;
};

/**
 * pass the entire _this.answers.databases[profile] ... into this one and generate the json
 */
module.exports.generate = function(virtual , connections)
{
	var json = {};
	_.each(connections , function(conf , profile)
	{
		var c = get(virtual , conf.databaseType , conf.databaseDriver);
		json[profile] = construct(c);
	});
	return JSON.stringify(json , null , 4);
};
